package com.example.guavaSrudy.basicStrings;

import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.io.UnsupportedEncodingException;
import java.util.Map;

public class GuavaBasicUse {

    public static void main(String[] args) {
//        StringsByte();
//        StringsByteUseGuava();
        testRemoveWhiteSpace();
//        testRetainFrom();
    }
//    来看看JAVA的string原生方法操作
    public static void StringsByte() {
//        try {
//            byte[] bytes2 = "foobarbaz".getBytes("UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        StringBuilder builder = new StringBuilder("foo");
        char c = 'x';
        for(int i=0; i<3; i++){
            builder.append(c);
        }
        System.out.println(builder.toString());
    }
// 使用guara来操作,代码就简洁了
    public static void StringsByteUseGuava() {
//        byte[] bytes2 = "foobarbaz".getBytes(Charsets.UTF_8);
        System.out.println(Strings.padEnd("foo", 6, 'x'));

    }


    public static void testRemoveWhiteSpace(){
        String tabsAndSpaces = " String  with      spaces     " +
                "and     tabs";
        String expected = "String with spaces and tabs";
        String scrubbed = CharMatcher.whitespace().collapseFrom(tabsAndSpaces,' ');
        String scrubbed2 = CharMatcher.whitespace().trimAndCollapseFrom(tabsAndSpaces,' ');
        System.out.println(scrubbed);
        System.out.println(scrubbed2);
    }

    public static void testRetainFrom(){
        String lettersAndNumbers = "foo989yx  bar234";
        String expected = "989234";
        String retained = CharMatcher.inRange('0','9').retainFrom(lettersAndNumbers);
        System.out.println(retained);
//       保留数字和空格
        CharMatcher cm = CharMatcher.inRange('0', '9').or(CharMatcher.whitespace());
        String retained2 = cm.retainFrom(lettersAndNumbers);
        System.out.println(retained2);
    }
}
